package Pre.Work;

import java.util.*;

public class Exam01 {
    //创建Student类，属性包括id[1-40],scroe[60-100], id属性⾃增，成绩属性随机⽣成。
    // 创建集合，保存20个对象，找到分数最⾼与最低的学⽣，使⽤List集合实现。
    private int id;
    private int score;

    public Exam01(int id, int score) {
        this.id = id;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Exam01{" +
                "学号：" + id +
                ", 成绩：" + score +
                '}';
    }

}
class Student{
    LinkedList list = new LinkedList();

    public void addList(int num){
        for (int i = 0; i < num ; i++) {
            Random random = new Random();
            int score = random.nextInt(40) + 60;
            list.add(new Exam01(i,score));
        }
    }

    public void scoreScore(){
         list.sort(new Comparator() {
             @Override
             public int compare(Object o1, Object o2) {
                 Exam01 e1 = (Exam01) o1;
                 Exam01 e2 = (Exam01) o2;
                 return e1.getScore() - e2.getScore();
             }
         });
    }

    public void showAll(){
        list.forEach(obj -> System.out.println(obj));
    }

    public static void main(String[] args) {
        Student stu = new Student();
        stu.addList(20);
        stu.scoreScore();
        stu.showAll();
        System.out.println("最高分：" + stu.list.getFirst());
        System.out.println("最低分：" + stu.list.getLast());
    }
}
